﻿var YorkinPage = {};
YorkinPage.InitializePage = function (elems) {
    function LoadGuest() {
        var url = $.url();
        var gid = url.fparam('gid');
        console.log(gid);
    }
    LoadGuest();
    $("#" + elems.GuestSearchButton).click(function () {
        $("#" + elems.GuestsTable).jtable('load', { searchString: $("#" + elems.GuestSearchInput).val() });
        return false;
    });
    $("#" + elems.GuestsTable).jtable({
        title: 'Guest\'s table',
        paging: true,
        pageSize: 10,
        sorting: true,
        defaultSorting: 'Name ASC',
        openChildAsAccordion: true,

        actions: {
            listAction: 'Services/GuestsService.asmx/JtableGuestControlPanelMain'
        },
        fields: {
            GuestID: {
                key: true,
                list: false
            },
            InvitesList: {
                title: '',
                sorting: false,
                width: '3%',
                display: function (data) {
                    var $img = $('<img style="cursor:pointer" src="img/list_metro.png" title="' + data.record.Name + '\'s invites" />');
                    var opened = false;
                    $img.click(function () {
                        if (opened) {
                            $("#" + elems.GuestsTable).jtable('closeChildTable', $img.closest('tr'));
                            opened = false;
                        } else {
                            opened = true;
                            $("#" + elems.GuestsTable).jtable('openChildTable',
                                $img.closest('tr'),
                                {
                                    title: data.record.Name + '\'s events.',
                                    actions: {
                                        listAction: 'Services/GuestsService.asmx/JtableGuestControlPanelChild?guestID=' + data.record.GuestID
                                    },
                                    fields: {
                                        ID: {
                                            key: true,
                                            list: false
                                        },
                                        EventName: {
                                            title: 'Event',
                                            width: '20%'
                                        }
                                    }
                                }, function (data) {
                                    data.childTable.jtable('load');
                                });
                        }
                    });

                    return $img;
                }
            },
            Name: {
                title: 'Name',
                width: '32%'
            },
            Email: {
                title: 'Email',
                width: '33'
            },
            Address: {
                title: 'Address',
                width: '33%',
                sorting: false
            },
            AvgGift: {
                title: 'Average Gift'
            }
        }
    });
    $("#" + elems.GuestsTable).jtable('load', { searchString: "" });
};